package com.cloudcreativity.blyg.ui.user;

import android.app.Activity;
import android.content.Intent;
import android.databinding.ObservableField;
import android.media.MediaPlayer;
import android.net.Uri;
import android.text.TextUtils;
import android.widget.CompoundButton;

import com.cloudcreativity.blyg.R;
import com.cloudcreativity.blyg.base.BaseDialogImpl;
import com.cloudcreativity.blyg.base.BaseModel;
import com.cloudcreativity.blyg.base.CommonWebActivity;
import com.cloudcreativity.blyg.databinding.ActivityLoginBinding;
import com.cloudcreativity.blyg.entity.UserEntity;
import com.cloudcreativity.blyg.ui.MainActivity;
import com.cloudcreativity.blyg.utils.APIService;
import com.cloudcreativity.blyg.utils.DefaultObserver;
import com.cloudcreativity.blyg.utils.HttpUtils;
import com.cloudcreativity.blyg.utils.SPUtils;
import com.cloudcreativity.blyg.utils.ToastUtils;
import com.google.gson.Gson;

import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class LoginModel extends BaseModel<Activity, ActivityLoginBinding> {

    public ObservableField<String> account = new ObservableField<>();
    public ObservableField<String> pwd = new ObservableField<>();
    private boolean isAgree = false;

    LoginModel(Activity context, ActivityLoginBinding binding, BaseDialogImpl baseDialog) {
        super(context, binding, baseDialog);
        binding.cbAgree.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                isAgree = b;
            }
        });
        //初始化，加载视频
        binding.vvLogin.setVideoURI(
                Uri.parse("android.resource://"+context.getPackageName()+"/"+ R.raw.mv_login_bg));
        binding.vvLogin.start();
        binding.vvLogin.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                mediaPlayer.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
                mediaPlayer.setLooping(true);
            }
        });
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    public void onForgetClick(){

    }

    public void login(){
        if(!isAgree){
            ToastUtils.showShortToast(context,"请先同意用户使用协议");
            return;
        }
        if(TextUtils.isEmpty(account.get())){
            ToastUtils.showShortToast(context,"帐号不能为空");
            return;
        }

        if(TextUtils.isEmpty(pwd.get())){
            ToastUtils.showShortToast(context,"密码不能为空");
            return;
        }

        HttpUtils.getInstance().login(account.get(),pwd.get())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DefaultObserver<UserEntity>(getBaseDialog(),true) {
                    @Override
                    public void onSuccess(UserEntity t) {
                        if(t.getInfo()!=null){
                            //保存登录信息，跳转到主页
                            SPUtils spUtils = SPUtils.get();
                            spUtils.setLogin(true);
                            spUtils.putString(SPUtils.Config.USER,new Gson().toJson(t.getInfo()));
                            context.startActivity(new Intent(context, MainActivity.class));
                            context.finish();
                        }
                    }
                });
    }

    public void onProtocolClick(){
        CommonWebActivity.startActivity(context,"用户使用协议",
                APIService.HOST_APP+"/base/userProtocol");
    }
}
